package com.chinasoft.mall.activity.adapter;

import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.chinasoft.mall.R;
import com.chinasoft.mall.activity.MainActivity;
import com.chinasoft.mall.model.response.RechnBean;
import com.chinasoft.mall.util.StringUtil;
import com.chinasoft.mall.widget.NetShadowImageView;

public class GallaryidImplAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private ViewHolder _vh;
	private List<RechnBean> rbs;

	public GallaryidImplAdapter(MainActivity activity, List<RechnBean> rbs) {
		inflater = LayoutInflater.from(activity);
		this.rbs = rbs;
	}

	public void setData(List<RechnBean> rbs) {
		this.rbs = rbs;
		this.notifyDataSetChanged();
	}
	
	public List<RechnBean> getData() {
		return rbs;
	}

	@Override
	public int getCount() {
		if (rbs == null) {
			return 0;
		} else {
			return rbs.size() / 3;
		}
	}

	public List<RechnBean> getRbs() {
		return rbs;
	}

	@Override
	public Object getItem(int position) {
		if (rbs == null) {
			return null;
		} else {
			return rbs.get(position * 3);
		}
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.gallerylv, null);
			_vh = new ViewHolder();
			_vh.gl_1 = (NetShadowImageView) convertView.findViewById(R.id.gl_1);
			_vh.tv_1 = (TextView) convertView.findViewById(R.id.tv_1);
			_vh.gl_2 = (NetShadowImageView) convertView.findViewById(R.id.gl_2);
			_vh.tv_2 = (TextView) convertView.findViewById(R.id.tv_2);
			_vh.gl_3 = (NetShadowImageView) convertView.findViewById(R.id.gl_3);
			_vh.tv_3 = (TextView) convertView.findViewById(R.id.tv_3);

			convertView.setTag(_vh);
		} else {
			_vh = (ViewHolder) convertView.getTag();
		}
		int _index = position * 3;
		int _max = rbs.size();
		if (_index + 1 <= _max) {
			RechnBean _rb = rbs.get(_index);
			_vh.tv_1.setText(_rb.getTITLE());
			_vh.gl_1.setDownloadurl(translateUrl(_rb));
//			_vh.gl_1.setOnClickListener(new onClickImpl(_rb));
		}
		_index++;
		if (_index + 1 <= _max) {
			RechnBean _rb = rbs.get(_index);
			_vh.tv_2.setText(_rb.getTITLE());
			_vh.gl_2.setDownloadurl(translateUrl(_rb));
//			_vh.gl_2.setOnClickListener(new onClickImpl(_rb));
		}
		_index++;
		if (_index + 1 <= _max) {
			RechnBean _rb = rbs.get(_index);
			_vh.tv_3.setText(_rb.getTITLE());
			_vh.gl_3.setDownloadurl(translateUrl(_rb));
//			_vh.gl_3.setOnClickListener(new onClickImpl(_rb));
		}
		return convertView;
	}
	
	public String translateUrl(RechnBean _rb){
		String type=_rb.getTYPE();
		if(type.equals("4")){
			return StringUtil.modelImageUrl(_rb.getID(),_rb.getTHUM());
		}else if(type.equals("3")){
			return StringUtil.grpBreviaryUrl(_rb.getID(),_rb.getTHUM());
		}else if(type.equals("5")){
			return StringUtil.goodsSpUrl(_rb.getID(),_rb.getTHUM());
		}else{
			return StringUtil.goodUrl(_rb.getID(),_rb.getTHUM());
		}
	}


	class ViewHolder {
		public NetShadowImageView gl_1;
		public TextView tv_1;
		public NetShadowImageView gl_2;
		public TextView tv_2;
		public NetShadowImageView gl_3;
		public TextView tv_3;
	}
}
